package com.fingard.text;

import java.util.Map;

public class JsonHelper {

	/**单层map(key:value)输出为页面能识别的list格式*/
	public static String mapToDataListJson(Map<String,String> pMap){
		StringBuilder sbJson = new StringBuilder();
    	sbJson.append("{");
    	sbJson.append("\"respCode\":" + "\"0\",\r\n");
        sbJson.append("\"respInfo\":" + "\"\",\r\n");
        
        sbJson.append("\"columns\":[");
        
        //List<String> keys = new ArrayList<String>(tmpMap.keySet());
        //for (String key : keys) {
        int cCount = 0;
        StringBuilder sb2 = new StringBuilder();
        for (Map.Entry<String, String> eachIt : pMap.entrySet()) {
        	if(cCount>0){
        		sbJson.append(",");
        		sb2.append(",");
        	}
        	sbJson.append("\""+eachIt.getKey()+"\"");
        	sb2.append("\"" + eachIt.getKey() + "\":\"" + eachIt.getValue().replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r") + "\"");
            cCount++;
        }
        sbJson.append("],\r\n");
        sbJson.append("\"dataList\":[");
        
        sbJson.append("{");
        sbJson.append(sb2.toString());
        sbJson.append("}]}");
        return sbJson.toString();
	}
}
